package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.UserFavoritesInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserFavoritesMapper {

    @Select("SELECT * FROM user_favorites")
    List<UserFavoritesInfo> findAll();

    @Select("SELECT * FROM user_favorites WHERE user_id = #{userId}")
    List<UserFavoritesInfo> findByUserId(Integer userId);

    @Select("SELECT * FROM user_favorites WHERE id = #{id}")
    UserFavoritesInfo findById(Integer id);

    @Insert("INSERT INTO user_favorites (user_id, question_id, create_time) VALUES (#{userId}, #{questionId}, NOW())")
    int insert(UserFavoritesInfo favorite);

    @Delete("DELETE FROM user_favorites WHERE id = #{id}")
    int deleteById(Integer id);

    @Delete("DELETE FROM user_favorites WHERE user_id = #{userId} AND question_id = #{questionId}")
    int deleteByUserAndQuestion(@Param("userId") Integer userId, @Param("questionId") Integer questionId);

    @Select("SELECT * FROM user_favorites WHERE user_id = #{userId} AND question_id = #{questionId}")
    UserFavoritesInfo findByUserAndQuestion(@Param("userId") Integer userId, @Param("questionId") Integer questionId);

    @Select({
            "<script>",
            "SELECT uf.id as favoriteId, uf.user_id, uf.question_id,",
            "       q.content AS question_content, q.analysis, q.answer,",
            "       kp.point_id, kp.point_name,",
            "       c.course_id, c.course_name,",
            "       uf.create_time",
            "FROM user_favorites uf",
            "LEFT JOIN questions q ON uf.question_id = q.question_id",
            "LEFT JOIN knowledge_points kp ON q.point_id = kp.point_id",
            "LEFT JOIN courses c ON kp.course_id = c.course_id",
            "WHERE uf.user_id = #{userId}",
            "<if test='courseId != null'> AND c.course_id = #{courseId} </if>",
            "<if test='pointId != null'> AND kp.point_id = #{pointId} </if>",
            "<if test='startTime != null and endTime != null'> AND uf.create_time BETWEEN #{startTime} AND #{endTime} </if>",
            "</script>"
    })
    List<Map<String, Object>> findFavoritesWithDetail(
            @Param("userId") Integer userId,
            @Param("courseId") Integer courseId,
            @Param("pointId") Integer pointId,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime
    );

    @Select({
            "<script>",
            "SELECT uf.id as favoriteId, uf.user_id, uf.question_id,",
            "       q.content AS question_content, q.analysis, q.answer,",
            "       kp.point_id, kp.point_name,",
            "       c.course_id, c.course_name,",
            "       uf.create_time",
            "FROM user_favorites uf",
            "LEFT JOIN questions q ON uf.question_id = q.question_id",
            "LEFT JOIN knowledge_points kp ON q.point_id = kp.point_id",
            "LEFT JOIN courses c ON kp.course_id = c.course_id",
            "WHERE uf.id IN",
            "  <foreach item='item' index='index' collection='list'",
            "           open='(' separator=',' close=')'>",
            "    #{item}",
            "  </foreach>",
            "</script>"
    })
    List<Map<String, Object>> findFavoritesByIds(@Param("list") List<Integer> ids);

}
